package com.botao.tip.controller;

import com.botao.tip.entity.LoginCount;
import com.botao.tip.service.FinanceService;
import com.botao.tip.service.LoginCountService;
import com.botao.tip.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : vip865047755@126.com
 * @version :JDK1.8
 * @date : 2020-12-04 22:06
 * @File : LoginCountController.java
 * @software: IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/loginCount/")
public class LoginCountController {

    @Autowired
    private LoginCountService loginCountService;

    @GetMapping("count")
    public AjaxResult count() {
        LoginCount loginCount = new LoginCount();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String result = sdf.format(new Date());
            List<LoginCount> loginCounts = loginCountService.queryByTime(result);
            if (loginCounts.isEmpty()) {
                loginCount.setTime(result);
                loginCount.setCounts(0);
                int i = loginCountService.insertCount(loginCount);
                if (i < 0) {
                    return AjaxResult.error("插入当前日期数据库错误。");
                }
            }

            return null;
        } catch (Exception e) {
            return AjaxResult.error("错误");
        }
    }
}
